package com.tarena.lbs.activity.serializer;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class MessageSerializer<T> {
    public byte[] serialize(T message) {
        String json = JSON.toJSONString(message);
        return json.getBytes(StandardCharsets.UTF_8);
    }

    public T deserialize(byte[] bytes, Class<T> clazz) {
        String json = new String(bytes, StandardCharsets.UTF_8);
        return JSON.parseObject(json, clazz);
    }

    public T deserialize(String json,Class<T> clazz){
        return JSON.parseObject(json,clazz);
    }

}
